const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://127.0.0.1:27017';
const dbName = 'myblog01';

MongoClient.connect(
    url,
    {
        useUnifiedTopology: true
    },
    (err, client) => {
        if (err) {
            console.error('mongodb clinent error')
            return;
        }
        console.log('mongodb client success')

        // 切换到指定数据库
        const db = client.db(dbName)

        // 操作集合
        const usersCollection = db.collection('users')
        usersCollection.find(
            {
                "username":'lishi'
            }
        ).toArray((err, result) => { 
            if(err){
                console.error('users find error')
                return
            }
            console.log(result)
            client.close();
        })

        // 关闭
        //client.close();

    }
)